package com.rsk.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.rsk.dao.SysUserLevelRepository;
import com.rsk.dao.SysUserRepository;
import com.rsk.dao.SysUserRoleRepository;
import com.rsk.entity.SysUser;
import com.rsk.entity.SysUserLevel;
import com.rsk.entity.SysUserRole;
import com.rsk.service.SysUserService;
import com.rsk.utils.algorithm.SnowflakeIdWorker;


@Service(interfaceClass = SysUserService.class)
public class SysUserServiceImpl implements SysUserService{
	
	@Autowired
	private SysUserRepository sysUserRrepository;
	
	@Autowired
	private SysUserLevelRepository sysUserLevelRepository;
	
	@Autowired
	private SysUserRoleRepository sysUserRoleRepository;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Transactional
	public Map<String, String> login(String userName) {
		Map<String, String> map = new HashMap<String, String>();
		List<Map> list = sysUserRrepository.login(userName);
		if(!list.isEmpty()&&list.size()>0){
			map = list.get(0);
		}
		return map;
	}

	@Transactional
	public int saveSysUser(String account, String passWord, String userName, Integer status, Long levelId, Long roleId, Long operatorUser) {
		//保存用户
		Long userId = SnowflakeIdWorker.generateId(true);
		SysUser sysUsers = new SysUser();
		sysUsers.setId(userId);
		sysUsers.setAccount(account);
		sysUsers.setPassWord(passWord);
		sysUsers.setUserName(userName);
		sysUsers.setStatus(status);
		sysUsers.setOperatorUser(operatorUser);
		sysUsers.setOperatorTime(new Date());
		sysUserRrepository.saveAndFlush(sysUsers);
		
		//保存用户级别
		SysUserLevel sysUserLevel = new SysUserLevel();
		sysUserLevel.setId(SnowflakeIdWorker.generateId(true));
		sysUserLevel.setUserId(userId);
		sysUserLevel.setLevelId(levelId);
		sysUserLevel.setOperatorUser(operatorUser);
		sysUserLevel.setOperatorTime(new Date());
		sysUserLevelRepository.saveAndFlush(sysUserLevel);
		
		//保存用户角色
		SysUserRole sysUserRole = new SysUserRole();
		sysUserRole.setId(SnowflakeIdWorker.generateId(true));
		sysUserRole.setUserId(userId);
		sysUserRole.setRoleId(roleId);
		sysUserRole.setOperatorUser(operatorUser);
		sysUserRole.setOperatorTime(new Date());
		sysUserRoleRepository.saveAndFlush(sysUserRole);
		return 1;
	}

	@Transactional
	public int updateSysUser(String account,String passWord,String userName,Integer status,Long operatorUser) {
		// TODO Auto-generated method stub
		return sysUserRrepository.updateSysUse(account, passWord,userName,status,operatorUser);
	}

	@Transactional
	public int delSysUser(Long id,  String account,Long operatorUser) {
		return sysUserRrepository.delSysUser(id, account,operatorUser);
	}

	@Transactional
	public int updateSysUserStatus(Long id,  String account, Integer status, Long operatorUser) {
		return sysUserRrepository.updateSysUserStatus(id, account, status);
	}
}
